package by.grsu.matrenichev.page.tutor;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.matrenichev.dao.TutorDao;
import by.grsu.matrenichev.entity.Tutor;
import by.grsu.matrenichev.page.BasePage;

public class EditTutorPage extends BasePage {

	@SpringBean
	private TutorDao tutorDao;

	@SuppressWarnings("serial")
	public EditTutorPage(final Tutor tutorToEdit) {
		super();

		// create form fields and set data
		final TextField<Integer> nField = new TextField<Integer>("n", new Model<Integer>(tutorToEdit.getN()));
		nField.setType(Integer.class);
		
		// create "form" component which is required for data retrieving from
		// client to server
		Form<Tutor> form = new Form<Tutor>("form", new Model<Tutor>(tutorToEdit)) {
			@Override
			public void onSubmit() {
				// retrieve data from the form and set it to the object
				tutorToEdit.setN(nField.getModelObject());
				// save updated object in DB
				tutorDao.save(tutorToEdit);
				// go to the list page
				setResponsePage(ListTutorPage.class);
			}
		};

		// add form fields
		form.add(nField);

		// add buttons
		form.add(new Button("saveLink"));
		form.add(new Link<Tutor>("cancelLink") {
			private static final long serialVersionUID = -1521748836027504873L;

			@Override
			public void onClick() {
				setResponsePage(ListTutorPage.class);
			}
		});
		// add form to the page
		add(form);

		// add special component which will allow to display "validation errors"
		// and feedback messages
		FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
	}

}
